package com.itheima.exception;

import com.itheima.pojo.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(TliasException.class)
    public Result abc(TliasException e){  //处理自定义的异常
        e.printStackTrace();
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result abc(Exception e){
        e.printStackTrace();
        return Result.error("服务器异常 , 请练习管理员");
    }
    @ExceptionHandler(DuplicateKeyException.class)
    public Result abc(DuplicateKeyException e){
        e.printStackTrace();
        if (e.getMessage().contains("emp.username")){
            return Result.error("用户名已存在");
        }
        if(e.getMessage().contains("emp.phone")){
            return Result.error("手机号已存在");
        }
        return Result.error("数据已存在");
    }

    @ExceptionHandler(NullPointerException.class)
    public Result abc(NullPointerException e){   //处理所有的空指针异常
        e.printStackTrace();
        return Result.error("服务器异常 , 请练习管理员");
    }

    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Result abc(ArrayIndexOutOfBoundsException e){   //处理数组数组索引越界异常
        e.printStackTrace();
        return Result.error("服务器异常 , 请练习管理员");
    }

}
